import { _decorator, Component, Node, Prefab, randomRangeInt, instantiate, v3, view, Size, UITransform, randomRange } from 'cc';
import { SysContant } from './constant/SysContant';
const { ccclass, property } = _decorator;

@ccclass('BulletBoomFactory')
export class BulletBoomFactory extends Component {
    
    @property(Prefab)
    bulletBufPrefab: Prefab
    
    @property(Prefab)
    boomBufPrefab: Prefab
    viewSize: Size;

    
    start() {
        this.viewSize = view.getVisibleSize()
        // this.generate()
    }

    update(deltaTime: number) {
        
    }

    /**
     * 
     * 随机一个时间间隔生成
     * 10-25生成一个
     * 
     */
    generate() {
        this.scheduleOnce(() => {
            this.generateOne()
            this.generate()
        // }, randomRangeInt(10, 25))
        }, randomRangeInt(3, 8))
    }

    generateOne() {

        
        
        if(SysContant.inst.gameTime <= 0) {
            return
        }
        
        let buffer = null;
        if(randomRangeInt(0, 2)) {
            buffer = instantiate(this.bulletBufPrefab)
        }else {
            buffer = instantiate(this.boomBufPrefab)
        }
        let maxX = this.viewSize.width / 2 - buffer.getComponent(UITransform).width / 2
        buffer.setPosition(v3(randomRange(-maxX, maxX), 600))
        
        buffer.setParent(this.node)
        
        
    }



}


